package com.infore.operation.controller;

import com.infore.operation.entity.*;
import com.infore.operation.exception.OperationException;
import com.infore.operation.service.FaultRecordService;
import com.infore.operation.utils.AbstractJsonObject;
import com.infore.operation.utils.FormatUtil;
import com.infore.operation.utils.StatusCode;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author:wk
 * @date:Created in 14:57 2018\4\17 0017
 */
@RestController
public class FaultRecordController {

    @Autowired
    private FaultRecordService faultRecordService;

    /**
     * 故障上报
     * @param faultId
     * @param equipId
     * @param findId
     * @param title
     * @param discrib
     * @param fttpid
     * @param time
     * @param isReport
     * @param HeadPsonId
     * @param code
     * @param isTest
     * @param buildTime
     * @param files
     * @return
     */
    @RequestMapping("/servlet/GZLR.do")
    public AbstractJsonObject saveFaultRecord(@RequestParam(value = "faultId",required = false)String faultId,
                                            @RequestParam(value = "equipId")String equipId,
                                            @RequestParam(value = "findId")String findId,
                                            @RequestParam(value = "title")String title,
                                            @RequestParam(value = "discrib")String discrib,
                                            @RequestParam(value = "fttpid")String fttpid,
                                            @RequestParam(value = "time")String time,
                                            @RequestParam(value = "isReport")String isReport,
                                            @RequestParam(value = "HeadPsonId",required = false)String HeadPsonId,
                                            @RequestParam(value = "code",required = false)String code,
                                            @RequestParam(value = "isTest",required = false)String isTest,
                                            @RequestParam(value = "buildTime",required = false)String buildTime,
                                            @RequestParam(value = "file")MultipartFile[] files){
        try {
            FormatUtil.secondG.parse(time);
            if (buildTime!=null){
                FormatUtil.secondG.parse(buildTime);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！（yyyy-MM-dd HH:mm:ss）");
        }
        String id = faultRecordService.saveFaultRecord(faultId,equipId,findId,title,discrib,fttpid,time,isReport,HeadPsonId,code,isTest,buildTime,files);
//        if ("1".equals(isReport)) {
            return FormatUtil.queryAbstractJsonObject("录入故障成功", StatusCode.CODE_SUCCESS);
//        }else {
//            return FormatUtil.queryAbstractJsonObject(id,1001);
//        }
    }

    /**
     * 故障列表
     * @param userId
     * @param pageNum
     * @return
     */
    @RequestMapping("/servlet/New_FAULT_RECD.do")
    public AbstractJsonObject queryNewFaultRecd(@RequestParam(value = "userId")String userId,
                                    @RequestParam(value = "pageNum")Integer pageNum){
        List<FaultList> list = faultRecordService.queryFaultList(userId,pageNum);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取故障列表成功");
    }

    /**
     * 故障详情
     * @param gzId
     * @return
     */
    @RequestMapping("/servlet/New_GZXQ.do")
    public AbstractJsonObject queryFaultRecordDetails(@RequestParam(value = "gzId")String gzId){
        FaultDetail detail = faultRecordService.queryFaultRecordDetails(gzId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("FaultRecd", detail);
        return FormatUtil.queryListObject(map,"获取故障详情成功");
    }

    /**
     * 故障报警查询
     * @param starttime
     * @param endtime
     * @param userId
     * @param faultTypeId
     * @param state0
     * @param state1
     * @param state2
     * @param pageNum
     * @param keyword
     * @return
     */
    @RequestMapping("/servlet/New_GZBJ.do")
    public AbstractJsonObject queryNewGZBJ(@RequestParam(value = "starttime")String starttime,
                               @RequestParam(value = "endtime")String endtime,
                               @RequestParam(value = "userId")String userId,
                               @RequestParam(value = "faultTypeId")String faultTypeId,
                               @RequestParam(value = "state0",required = false)String state0,
                               @RequestParam(value = "state1",required = false)String state1,
                               @RequestParam(value = "state2",required = false)String state2,
                               @RequestParam(value = "pageNum")Integer pageNum,
                               @RequestParam(value = "keyword",required = false)String keyword){
        try {
            FormatUtil.secondA.parse(starttime);
            FormatUtil.secondA.parse(endtime);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！（yyyy-MM-dd）");
        }
        List<FaultList> list = faultRecordService.searchFault(starttime,endtime,userId,faultTypeId,state0,state1,state2,pageNum,keyword);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取故障报警成功");
    }

    /**
     * 根据单号id获取详情
     * @param mtPcsId
     * @return
     */
    @RequestMapping("/servlet/queryMtPcs")
    public AbstractJsonObject queryMtPcs(@RequestParam(value = "mtPcsId")String mtPcsId){
        Map<String,Object > mapDetail = faultRecordService.queryMtPcs(mtPcsId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("mslist", mapDetail);
        return FormatUtil.queryListObject(map,"获取配件库存信息成功");
    }

    /**
     * 故障类型
     * @return
     */
    @RequestMapping("/servlet/FAULT_TYPE.do")
    public AbstractJsonObject queryFaultType(){
        List<FaultType> list = faultRecordService.queryFaultType();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("FaultType", list);
        return FormatUtil.queryListObject(map,"获取故障类型成功");
    }

    /**
     * 删除故障
     * @param faultId
     * @return
     */
    @RequestMapping("/servlet/DeleteFault.do")
    public  AbstractJsonObject deleteFault(@RequestParam(value = "faultId")String faultId){
        faultRecordService.deleteFault(faultId);
        return  FormatUtil.queryAbstractJsonObject("删除故障成功",StatusCode.CODE_SUCCESS);
    }

    /**
     * 故障上报列表
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/GZSBLIST.do")
    public AbstractJsonObject queryFaultRecdList(@RequestParam(value = "userId")String userId,
                                                @RequestParam(value = "pageNum",required = false,defaultValue = "0")Integer pageNum){
        List<FaultRecdList> list = faultRecordService.queryFaultRecdList(userId,pageNum);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("FaultRecdlist", list);
        return FormatUtil.queryListObject(map,"获取故障上报列表成功");
    }

    /**
     * 故障上报详情
     * @param gzId
     * @return
     */
    @RequestMapping("/servlet/SBXX.do")
    public AbstractJsonObject queryFaultRecdDetails(@RequestParam(value = "gzId")String gzId){
        FaultRecord record = faultRecordService.queryFaultRecdDetails(gzId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("FaultRecd", record);
        return FormatUtil.queryListObject(map,"获取故障上报详情成功");
    }

    /**
     * 故障审批
     * @param faultId
     * @param state
     * @param time
     * @param APId
     * @param HDId
     * @param discrib
     * @param headPsonId
     * @param faultApprovalId
     * @return
     */
    @RequestMapping("/servlet/GZSH.do")
    public AbstractJsonObject queryFaultRecdApprove(@RequestParam(value = "faultId",required = false)String faultId,
                                        @RequestParam(value = "state")String state,
                                        @RequestParam(value = "time")String time,
                                        @RequestParam(value = "APId")String APId,
                                        @RequestParam(value = "HDId",required = false)String HDId,
                                        @RequestParam(value = "discrib")String discrib,
                                        @RequestParam(value = "HeadPsonId",required = false)String headPsonId,
                                        @RequestParam(value = "faultApprovalId",required = false)String faultApprovalId){
        try {
            FormatUtil.secondG.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！(yyyy-MM-dd HH:mm:ss)");
        }
        faultRecordService.queryFaultRecdApprove(faultId,state,time,APId,HDId,discrib,headPsonId,faultApprovalId);
        return  FormatUtil.queryAbstractJsonObject("故障审批成功",StatusCode.CODE_SUCCESS);
    }

    /**
     * 检修列表
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/JXList.do")
    public AbstractJsonObject queryReconditionList(@RequestParam(value = "userId") String userId,
                                                @RequestParam(value = "pageNum",required = false,defaultValue = "0")Integer pageNum){
        List<FixList> list = faultRecordService.queryReconditionList(userId,pageNum);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("falist", list);
        return FormatUtil.queryListObject(map,"获取检修列表成功");
    }

    /**
     * 故障级别
     * @return
     */
    @RequestMapping("/servlet/GZJB.do")
    public AbstractJsonObject queryFaultLevel(){
        List<FaultLevel> list = faultRecordService.queryFaultLevel();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("gz", list);
        return FormatUtil.queryListObject(map,"获取故障级别成功");
    }

    /**
     * 故障类型
     * @return
     */
    @RequestMapping("/servlet/JXModel.do")
    public AbstractJsonObject queryReconditionModel(){
        List<FaultLevel> list = faultRecordService.queryReconditionModel();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("model", list);
        return FormatUtil.queryListObject(map,"获取故障类型成功");
    }

    /**
     * 检修录入
     * @param faId
     * @param starttime
     * @param result
     * @param userId
     * @param endtime
     * @param faultLevel
     * @param faultType
     * @param timel
     * @param states
     * @param autostate
     * @param partNum
     * @param partList
     * @param picture
     * @return
     */
    @RequestMapping("/servlet/JXLR.do")
    public AbstractJsonObject queryRecondition(@RequestParam(value = "faId")String faId,
                                   @RequestParam(value = "starttime")String starttime,
                                   @RequestParam(value = "result")String result,
                                   @RequestParam(value = "userId")String userId,
                                   @RequestParam(value = "endtime")String endtime,
                                   @RequestParam(value = "faultLevel")String faultLevel,
                                   @RequestParam(value = "faultType")String faultType,
                                   @RequestParam(value = "timel")String timel,
                                   @RequestParam(value = "states",required = false)String states,
                                   @RequestParam(value = "autostate",required = false)String autostate,
                                   @RequestParam(value = "partNum",required = false)String partNum,
                                   @RequestParam(value = "partList",required =false)String partList,
                                   @RequestParam(value = "picture",required = false)MultipartFile[] picture){
        try {
            FormatUtil.secondG.parse(starttime);
            FormatUtil.secondG.parse(endtime);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！(yyyy-MM-dd HH:mm:ss)");
        }
        try {
            faultRecordService.queryRecondition(faId,starttime,result,userId,endtime,faultLevel,faultType,timel,states,autostate,partNum,partList,picture);
        } catch (IOException e) {
            e.printStackTrace();
            return  FormatUtil.queryAbstractJsonObject("检修录入失败",StatusCode.CODE_ERROR);
        }
        return  FormatUtil.queryAbstractJsonObject("检修录入成功",StatusCode.CODE_SUCCESS);
    }


    /**
     * 检修审核列表
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/JXSHList.do")
    public AbstractJsonObject queryReconditionCheckList(@RequestParam(value = "userId") String userId){
        List<RstList> list = faultRecordService.queryReconditionCheckList(userId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ohrstlist", list);
        return FormatUtil.queryListObject(map,"获取检修审核列表成功");
    }

    /**
     * 检修审批
     * @param orId
     * @param state
     * @param userId
     * @param time
     * @param discrib
     * @return
     */
    @RequestMapping("/servlet/JXSH.do")
    public AbstractJsonObject queryReconditionCheck(@RequestParam(value = "orId") String orId,
                                        @RequestParam(value = "state") String state,
                                        @RequestParam(value = "userId") String userId,
                                        @RequestParam(value = "time") String time,
                                        @RequestParam(value = "discrib") String discrib){
        try {
            FormatUtil.secondG.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！(yyyy-MM-dd HH:mm:ss)");
        }
        faultRecordService.queryReconditionCheck(orId,state,userId,time,discrib);
        return  FormatUtil.queryAbstractJsonObject("检修审批成功",StatusCode.CODE_SUCCESS);
    }

    /**
     * 巡检列表
     * @param userId
     * @param lastId
     * @param stationId
     * @return
     */
    @RequestMapping("/servlet/getInspectHistoryList.do")
    public AbstractJsonObject getInspectHistoryList( @RequestParam(value = "userId") String userId,
                                         @RequestParam(value = "lastId") String lastId,
                                         @RequestParam(value = "stationId") String stationId){
        List<Inspect> list = faultRecordService.getInspectHistoryList(userId,lastId,stationId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取巡检列表成功");
    }
    /**
     * 查询巡检历史
     * @param startTime
     * @param lastId
     * @param stationId
     * @param endTime
     * @return
     */
    @RequestMapping("/servlet/queryInspectHistory.do")
    public AbstractJsonObject queryInspectHistory(@RequestParam(value = "startTime") String startTime,
                                      @RequestParam(value = "lastId") String lastId,
                                      @RequestParam(value = "stationId") String stationId,
                                      @RequestParam(value = "endTime") String endTime){
        try {
            FormatUtil.secondA.parse(startTime);
            FormatUtil.secondA.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new OperationException("时间格式有误！(yyyy-MM-dd)");
        }
        List<Inspect> list = faultRecordService.searchInspectHistory(startTime,lastId,stationId,endTime);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取巡检历史成功");
    }


    /**
     * 删除巡检信息
     * @param inspectId
     * @return
     */
    @RequestMapping("/servlet/deleteInspectHistory.do")
    public AbstractJsonObject deleteInspectHistory( @RequestParam(value = "inspectId") String inspectId){
        faultRecordService.deleteInspectHistory(inspectId);
        return  FormatUtil.queryAbstractJsonObject("删除巡检信息成功",StatusCode.CODE_SUCCESS);
    }

    /**
     * 巡检详情
     * @param stationId
     * @return
     */
    @RequestMapping("/servlet/XJXX.do")
    public AbstractJsonObject queryInspectDetails( @RequestParam(value = "stationId") String stationId){
        InspectDetail detail = faultRecordService.queryInspectDetails(stationId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("FaultRecd", detail);
        return FormatUtil.queryListObject(map,"获取巡检详情成功");
    }

    /**
     * 根据出库单获取
     * @param MtPcsId
     * @return
     */
    @RequestMapping("/servlet/MtPcsBynum.do")
    public AbstractJsonObject queryMtPcsBynum(@RequestParam(value = "MtPcsId") String MtPcsId){
        Object obj = faultRecordService.queryMtPcsBynum(MtPcsId);
        if(obj.toString().equals("2000")){
            return  FormatUtil.queryAbstractJsonObject("获取物料单已被使用",2000);
        }else if(obj.toString().equals("1000")){
            return  FormatUtil.queryAbstractJsonObject("出库单获取失败",1000);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("MtPcsDetail", obj);
        return FormatUtil.queryListObject(map,"出库单获取成功");
    }

    /**
     * 根据物料编码和描述查询
     * @param keyword
     * @param lastId
     * @param userId
     * @return
     */
    @RequestMapping("/servlet/Ports.do")
    public AbstractJsonObject queryPorts(@RequestParam(value = "keyword") String keyword,
                             @RequestParam(value = "lastId") String lastId,
                             @RequestParam(value = "userId") String userId){
        List<Stock> list = faultRecordService.queryPorts(keyword,lastId,userId);
        if(list==null||list.size()==0){
            return FormatUtil.queryAbstractJsonObject("没有找到相应物料", StatusCode.CODE_ERROR);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("mtstock", list);
        return FormatUtil.queryListObject(map,"获取物料成功");
    }
    /**
     * 获取配件库存信息
     * @param lastId
     * @param brId
     * @return
     */
    @RequestMapping("/servlet/MtStock.do")
    public AbstractJsonObject queryMtStock(@RequestParam(value = "lastId") String lastId,
                               @RequestParam(value = "brId") String brId){
        List<NStock> list = faultRecordService.queryMtStock(lastId,brId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("mslist", list);
        return FormatUtil.queryListObject(map,"获取配件库存信息成功");
    }

    /**
     * 查询配件库存信息
     * @param brId
     * @param mskeyword
     * @return
     */
    @RequestMapping("/servlet/SearchMtStock.do")
    public AbstractJsonObject querySearchMtStock(@RequestParam(value = "brId") String brId,
                                     @RequestParam(value = "mskeyword") String mskeyword){
        List<NStock> list = faultRecordService.searchMtStock(mskeyword,brId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("mslist", list);
        return FormatUtil.queryListObject(map,"获取配件库存信息成功");
    }

    /**
     * 巡检
     * @param userId
     * @param stationId
     * @param startTime
     * @param endTime
     * @param describe
     * @param pictures
     * @param faultList
     * @param partNum
     * @param partList
     * @param lat
     * @param lnt
     * @param address
     * @return
     */
    @RequestMapping(value = {"/servlet/DailyInspect","/servlet/servlet/DailyInspect"})
    public AbstractJsonObject queryDailyInspect(@RequestParam(value = "userId") String userId,
                                    @RequestParam(value = "stationId") String stationId,
                                    @RequestParam(value = "startTime") String startTime,
                                    @RequestParam(value = "endTime") String endTime,
                                    @RequestParam(value = "describe") String describe,
                                    @RequestParam(value = "picture",required = false) MultipartFile[] pictures,
                                    @RequestParam(value = "faultImage",required = false) MultipartFile[] faultImage,
                                    @RequestParam(value = "faultList",required =false) String faultList,
                                    @RequestParam(value = "partNum",required = false) String partNum,
                                    @RequestParam(value = "partList",required = false) String partList,
                                    @RequestParam(value = "lat") String lat,
                                    @RequestParam(value = "lnt") String lnt,
                                    @RequestParam(value = "address") String address){
            try {
                FormatUtil.secondG.parse(startTime);
                FormatUtil.secondG.parse(endTime);
            } catch (ParseException e) {
                e.printStackTrace();
                throw new OperationException("时间格式有误！(yyyy-MM-dd HH:mm:ss)");
            }
        try {
            faultRecordService.queryDailyInspect(faultImage,userId,stationId,startTime,endTime,describe,pictures,faultList,partNum,partList,lat,lnt,address
            );
        } catch (IOException e) {
            e.printStackTrace();
            return  FormatUtil.queryAbstractJsonObject("巡检录入失败",StatusCode.CODE_ERROR);
        }
        return  FormatUtil.queryAbstractJsonObject("巡检录入成功",StatusCode.CODE_SUCCESS);
    }

    /**
     * 根据检修id获取详情
     * @param fixId
     * @return
     */
    @RequestMapping("/servlet/getFixDetail")
    public AbstractJsonObject getFixDetail(@RequestParam(value = "fixId")Integer fixId){
        FixDetail detail = faultRecordService.getFixDetail(fixId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("mslist", detail);
        return FormatUtil.queryListObject(map,"获取配件库存信息成功");
    }
}
